package kmp;

public class leetCode557_reverseWords {
    public String reverseWords(String s) {
        if(s == null || s.length() < 2) return s;
        StringBuilder sb = new StringBuilder();
        int n = s.length();
        int i = 0;
        while (i < n){
            int start = i;
            while (i < n && s.charAt(i) != ' '){
                i++;
            }
            for (int j = start; j < i; j++){
                sb.append(s.charAt(i + start - j - 1));
            }
            while (i < n && s.charAt(i) == ' '){
                sb.append(" ");
                i++;
            }
        }
        return sb.toString();

    }
}
